﻿using System;
using System.Data;
using UniMana.Utils.Configs;
using UniMana.Utils.Data;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public partial class FrmViewSchedule_Lecturer : DevExpress.XtraEditors.XtraForm
    {
        Cls_ConvertDataToSchedule Con = new Cls_ConvertDataToSchedule();

        private DataTable m_tblTable;
        private DBMain m_data = new DBMain();
        private string m_personalKey = "";
        private DateTime m_dStartTime;
        private DateTime m_dEndTime;

        public DevExpress.Utils.WaitDialogForm dlg;

        public FrmViewSchedule_Lecturer()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmViewSchedule_Lecturer_Load(object sender, EventArgs e)
        {
            //XtraMessageBox.Show("Mã giảng viên đề nghị test " + "DLA0069");
            LoadData();
        }

        /// <summary>
        /// Khởi tạo dữ liệu
        /// </summary>

        private void LoadData()
        {
            m_tblTable = new DataTable();
            m_tblTable = (this.m_data.m_DCReadOnly.Personnel_Personnels.GetListLecturer()).ToDataTable();
            if (m_tblTable.Rows.Count != 0)
                gluPersonnel_Personnel.GetData(m_tblTable, "Personnel_PersonnelKey", "FullName");

            m_dStartTime = SysDateTime.Now - new TimeSpan(30, 0, 0, 0);
            m_dEndTime = SysDateTime.Now + new TimeSpan(30, 0, 0, 0);
            dteStart.EditValue = m_dStartTime;
            dteEnd.EditValue = m_dEndTime;
        }

        /// <summary>
        /// Kiểm tra đầy đủ thông tin để xuất dữ liệu
        /// </summary>
        /// <returns></returns>

        private bool CheckInfo(object glu, object dtestart, object dteend)
        {
            if ((glu != null) && (dtestart != null) && (dteend != null))
                return true;
            return false;
        }

        /// <summary>
        /// Lấy thời khóa biểu giảng viên
        /// </summary>

        private void LoadSchedulePerson()
        {
            dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
            m_tblTable = new DataTable();
            m_tblTable = (this.m_data.m_DCReadOnly.Training_Session_Subject_Class_Rooms.GetDataSchedulePersonWithTime(m_data.m_DCReadOnly.Training_Sessions,
                m_data.m_DCReadOnly.Training_Subject_Classes,
                m_data.m_DCReadOnly.Personnel_Personnels,
                m_data.m_DCReadOnly.Training_WeekDays,
                m_data.m_DCReadOnly.Training_Class_Rooms,
                m_data.m_DCReadOnly.Training_Subjects,
                m_dStartTime,
                m_dEndTime,
                m_personalKey)).ToDataTable();

            if (m_tblTable.Rows.Count != 0)
            {
                // Sắp xếp lại dữ liệu
                DataView dv = m_tblTable.DefaultView;
                dv.Sort = "RegistTime ASC";
                m_tblTable = dv.ToTable();
                // Chuyển đổi dữ liệu                
                gctView.DataSource = Con.Implementation(m_tblTable);
                // Kết thúc chuyển đổi dữ liệu
                dlg.Close();
            }
            else
            {
                dlg.Close();
                XtraMessageBox.Show("Không có lịch công tác trong thời gian chọn");
                gctView.DataSource = null;
            }
        }

        /// <summary>
        /// Chọn giảng viên xem lịch công tác
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluPersonnel_Personnel_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                m_personalKey = gluPersonnel_Personnel.EditValue.ToString();
            }
            catch { m_personalKey = ""; }
        }

        /// <summary>
        /// Chọn hôm nay
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lbcNow_Click(object sender, EventArgs e)
        {
            dteStart.EditValue = dteEnd.EditValue = SysDateTime.Now;
        }

        /// <summary>
        /// Chọn ngày bắt đầu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void dteStart_EditValueChanged(object sender, EventArgs e)
        {
            m_dStartTime = (DateTime)dteStart.EditValue;
        }

        /// <summary>
        /// Chọn ngày kết thúc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void dteEnd_EditValueChanged(object sender, EventArgs e)
        {
            m_dEndTime = (DateTime)dteEnd.EditValue;
        }

        private void lbcNow_MouseHover(object sender, EventArgs e)
        {
            this.lbcNow.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline | System.Drawing.FontStyle.Italic))));
        }

        private void lbcNow_MouseLeave(object sender, EventArgs e)
        {
            this.lbcNow.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline))));
        }

        private void grvView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView View = sender as GridView;
            if (e.Column.FieldName == "Ngày")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Ngày"]);
                if (category == "Ngày")
                    //e.Appearance.BackColor = Color.DeepSkyBlue;
                    e.Appearance.BackColor = Color.Silver;
                else
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Buổi")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Buổi"]);
                if (category == "Buổi")
                    e.Appearance.BackColor = Color.Silver;
                else
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 2")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 2"]);
                if (category == "Thứ 2")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 3")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 3"]);
                if (category == "Thứ 3")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 4")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 4"]);
                if (category == "Thứ 4")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 5")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 5"]);
                if (category == "Thứ 5")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 6")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 6"]);
                if (category == "Thứ 6")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Thứ 7")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Thứ 7"]);
                if (category == "Thứ 7")
                    e.Appearance.BackColor = Color.Silver;
            }
            if (e.Column.FieldName == "Chủ nhật")
            {
                string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Chủ nhật"]);
                if (category == "Chủ nhật")
                    e.Appearance.BackColor = Color.Silver;
            }
        }

        /// <summary>
        /// Khởi tạo gridview
        /// </summary>

        private void InitalGridView()
        {
            // gridband1
            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl0;
            gcl0 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl0 });
            gcl0.Caption = "Ngày";
            gcl0.Name = "gclNgay";
            gcl0.FieldName = "Ngày";
            gcl0.Width = 150;
            gcl0.Visible = true;
            gcl0.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl0.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl0);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl1;
            gcl1 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl1 });
            gcl1.Caption = "Buổi";
            gcl1.Name = "gclBuoi";
            gcl1.FieldName = "Buổi";
            gcl1.Width = 100;
            gcl1.Visible = true;
            gcl1.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl1.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl1);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl2;
            gcl2 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl2 });
            gcl2.Caption = "Thứ 2";
            gcl2.Name = "gclThu2";
            gcl2.FieldName = "Thứ 2";
            gcl2.Width = 200;
            gcl2.Visible = true;
            gcl2.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl2.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl2);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl3;
            gcl3 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl3 });
            gcl3.Caption = "Thứ 3";
            gcl3.Name = "gclThu3";
            gcl3.FieldName = "Thứ 3";
            gcl3.Width = 200;
            gcl3.Visible = true;
            gcl3.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl3.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl3);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl4;
            gcl4 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl4 });
            gcl4.Caption = "Thứ 4";
            gcl4.Name = "gclThu4";
            gcl4.FieldName = "Thứ 4";
            gcl4.Width = 200;
            gcl4.Visible = true;
            gcl4.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl4.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl4);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl5;
            gcl5 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl5 });
            gcl5.Caption = "Thứ 5";
            gcl5.Name = "gclThu5";
            gcl5.FieldName = "Thứ 5";
            gcl5.Width = 200;
            gcl5.Visible = true;
            gcl5.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl5.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl5);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl6;
            gcl6 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl6 });
            gcl6.Caption = "Thứ 6";
            gcl6.Name = "gclThu6";
            gcl6.FieldName = "Thứ 6";
            gcl6.Width = 200;
            gcl6.Visible = true;
            gcl6.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl6.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl6);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl7;
            gcl7 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl7 });
            gcl7.Caption = "Thứ 7";
            gcl7.Name = "gclThu7";
            gcl7.FieldName = "Thứ 7";
            gcl7.Width = 200;
            gcl7.Visible = true;
            gcl7.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl7.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl7);

            DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn gcl8;
            gcl8 = new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn();
            grvView.Columns.AddRange(new DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn[] { gcl8 });
            gcl8.Caption = "Chủ nhật";
            gcl8.Name = "gclChuNhat";
            gcl8.FieldName = "Chủ nhật";
            gcl8.Width = 200;
            gcl8.Visible = true;
            gcl8.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            gcl8.AppearanceCell.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
            grb.Columns.Add(gcl8);
        }

        private void xucBar1_btnAddNew_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (CheckInfo(gluPersonnel_Personnel.EditValue, dteStart.EditValue, dteEnd.EditValue))
                LoadSchedulePerson();
            else
            {
                XtraMessageBox.Show("Chưa chọn giảng viên hoặc thời gian");
                gctView.DataSource = null;
            }
        }

        private void xucBar1_btnExport_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "*.xlsx | *.xls";
            saveFile.Title = "Lưu tập tin excel";
            if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grvView.ExportToXlsx(saveFile.FileName);
            }
        }

        private void xucBar1_btnPrint_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gclNgay.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclNgay.AppearanceHeader.Options.UseForeColor = true;
            this.gclBuoi.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclBuoi.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu2.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu2.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu3.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu3.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu4.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu4.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu5.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu5.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu6.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu6.AppearanceHeader.Options.UseForeColor = true;
            this.gclThu7.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclThu7.AppearanceHeader.Options.UseForeColor = true;
            this.gclChuNhat.AppearanceHeader.ForeColor = System.Drawing.Color.White;
            this.gclChuNhat.AppearanceHeader.Options.UseForeColor = true;

            string strTenlop = gluPersonnel_Personnel.Text.ToUpper();
            this.printableComponentLink1.ClearDocument();
            this.printableComponentLink1.RtfReportHeader = this.printableComponentLink1.RtfReportHeader.Replace("giangvien", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(strTenlop));
            string ngaythang = "Tân An," + " ngày" + " " + DateTime.Now.Day + " " + "tháng" + " " + DateTime.Now.Month + " " + "năm" + " " + DateTime.Now.Year;
            string nguoidung = SysGlobalVar.g_sta_sysUserLogin.Properties.FullName;
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("nguoilap", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(nguoidung));
            this.printableComponentLink1.RtfReportFooter = this.printableComponentLink1.RtfReportFooter.Replace("ngaythang", UniMana.Utils.Helper.String.GetRtfUnicodeEscapedString(ngaythang));
            printableComponentLink1.ShowPreview();

            this.gclNgay.AppearanceHeader.Options.UseForeColor = false;
            this.gclBuoi.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu2.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu3.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu4.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu5.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu6.AppearanceHeader.Options.UseForeColor = false;
            this.gclThu7.AppearanceHeader.Options.UseForeColor = false;
            this.gclChuNhat.AppearanceHeader.Options.UseForeColor = false;
        }
    }
}